package com.examples.infobox;

import org.eclipse.jface.viewers.ArrayContentProvider;
import org.eclipse.jface.viewers.ColumnWeightData;
import org.eclipse.jface.viewers.ISelectionChangedListener;
import org.eclipse.jface.viewers.SelectionChangedEvent;
import org.eclipse.jface.viewers.TableLayout;
import org.eclipse.jface.viewers.TableViewer;
import org.eclipse.jface.viewers.TableViewerColumn;
import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.SashForm;
import org.eclipse.swt.custom.StyledText;
import org.eclipse.swt.events.FocusEvent;
import org.eclipse.swt.events.FocusListener;
import org.eclipse.swt.events.KeyAdapter;
import org.eclipse.swt.events.KeyEvent;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.ExpandBar;
import org.eclipse.swt.widgets.ExpandItem;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.Text;

import com.examples.infobox.ExampleData.Value;

/**
 * 
 * @author ryada9
 * 
 */

public class LeftComposite {

	public static void createControl(final SashForm sashForm) {
		final Display display = sashForm.getDisplay();
		// Create Left composite
		final Composite leftCompositeHolder = new Composite(sashForm, SWT.BORDER);
		// shell for pop-up table on left composite.
		final Shell tableShell = new Shell(display, SWT.ON_TOP);

		// Create Right Composite
		final Composite rightInfoBoxComposite = new Composite(sashForm,
				SWT.BORDER);
		rightInfoBoxComposite.setBackground(new Color(null, 255, 255, 255));

		/* Uncomment to use Sash Composite and manage your own SashForm
		// Create Left composite
		final Composite leftCompositeHolder = new Composite(
				sashForm.getParent(), SWT.NONE);

		Sash sash = new Sash(sashForm.getParent(), SWT.VERTICAL);
		final FormData sashData = new FormData();
		sashData.left = new FormAttachment(30);
		sashData.top = new FormAttachment(0);
		sashData.bottom = new FormAttachment(100);
		sash.setLayoutData(sashData);
		sash.addListener(SWT.Selection, new Listener() {
			public void handleEvent(Event event) {
				if (event.detail != SWT.DRAG) {
					sashData.left = new FormAttachment(0, event.x);
					sashForm.getParent().layout();
				}
			}
		});
		
		// Create Right Composite
		final Composite rightInfoBoxComposite = new Composite(
				sashForm.getParent(), SWT.NONE);
		rightInfoBoxComposite.setBackground(new Color(null, 255, 255, 255));

		FormLayout formLayout = new FormLayout();
		sashForm.getParent().setLayout(formLayout);
		FormData data2 = new FormData();
		data2.left = new FormAttachment(sash);
		data2.right = new FormAttachment(100);
		data2.top = new FormAttachment(0);
		data2.bottom = new FormAttachment(100);
		rightInfoBoxComposite.setLayoutData(data2);
		
		FormData listData = new FormData();
		listData.left = new FormAttachment(0);
		listData.right = new FormAttachment(sash);
		listData.top = new FormAttachment(0);
		listData.bottom = new FormAttachment(100);
		leftCompositeHolder.setLayoutData(listData);
		*/

		// Populate Left Composite
		GridLayout layout = new GridLayout();
		layout.numColumns = 2;
		leftCompositeHolder.setLayout(layout);

		final Label account = new Label(leftCompositeHolder, SWT.NO);
		account.setText("Account");
		account.pack();
		final Text autoComText = new Text(leftCompositeHolder, SWT.NO);
		autoComText.pack();

		final Label security = new Label(leftCompositeHolder, SWT.NO);
		security.setText("Security");
		security.pack();
		final Text securityText = new Text(leftCompositeHolder, SWT.NO);
		securityText.pack();

		// Set layout for tableShell
		FillLayout layout1 = new FillLayout();
		tableShell.setLayout(layout1);

		// set bounds for tableShell
		sashForm.getParent().addListener(SWT.Move, new Listener() {
			public void handleEvent(Event e) {
				Rectangle rect = autoComText.getBounds();
				rect = display.map(leftCompositeHolder, null, rect);
				rect.y += rect.height;
				rect.height = 200;
				rect.width = 200;
				tableShell.setBounds(rect);
			}
		});

		// create table
		TableLayout tableLayout = new TableLayout();
		tableLayout.addColumnData(new ColumnWeightData(1));
		tableLayout.addColumnData(new ColumnWeightData(1));
		Table exampleTable = new Table(tableShell, SWT.BORDER
				| SWT.FULL_SELECTION | SWT.V_SCROLL);
		exampleTable.setLinesVisible(true);
		exampleTable.setHeaderVisible(true);
		exampleTable.setLayout(tableLayout);
		final TableViewer exampleTableViewer = new TableViewer(exampleTable);
		exampleTableViewer.getControl().setLayoutData(
				new GridData(SWT.FILL, SWT.FILL, true, true));
		TableViewerColumn labelColumn = new TableViewerColumn(
				exampleTableViewer, SWT.NONE);
		labelColumn.getColumn().setText("Label");
		TableViewerColumn valueColumn = new TableViewerColumn(
				exampleTableViewer, SWT.NONE);
		valueColumn.getColumn().setText("Value");

		exampleTableViewer.setContentProvider(new ArrayContentProvider());
		//exampleTableViewer.setLabelProvider(new ExampleTableLabelProvider());

		// add filter
		final ExampleDataFilter tableFilter = new ExampleDataFilter();
		exampleTableViewer.addFilter(tableFilter);

		// set input
		ExampleData[] input = new ExampleData[] {
				new ExampleData("Label 1", Value.value1),
				new ExampleData("Label 2", Value.value2),
				new ExampleData("B1", Value.value3),
				new ExampleData("S1", Value.value1),
				new ExampleData("A1", Value.value1),
				new ExampleData("D1", Value.value1) };

		exampleTableViewer.setInput(input);

		exampleTableViewer
				.addSelectionChangedListener(new ISelectionChangedListener() {
					// create populate right composite(Info Box).
					@Override
					public void selectionChanged(SelectionChangedEvent event) {
						TableViewer tb = (TableViewer) event.getSource();
						ExampleData e = (ExampleData) tb.getElementAt(tb
								.getTable().getSelectionIndex());
						if (e != null) {
							autoComText.setText(e.getLabel());
							/*
							 * createRightComposite(rightInfoBoxComposite,
							 * e.getLabel());
							 */
							createRightComposite1(rightInfoBoxComposite,
									e.getLabel());
						}
						tableShell.setVisible(false);
					}
				});

		// *****
		// Was trying to do this to make table dialog disappear if user clicks
		// anywhr out of table.
		// But not working.
		final FocusListener focusListener = new FocusListener() {

			@Override
			public void focusGained(FocusEvent arg0) {
				// TODO Auto-generated method stub

			}

			@Override
			public void focusLost(FocusEvent arg0) {
				// tableShell.setVisible(false); //Not working
			}

		};

		KeyAdapter listener = new KeyAdapter() {

			@Override
			public void keyReleased(KeyEvent evt) {
				String inputText = autoComText.getText();
				tableFilter.setSearchText(inputText);
				exampleTableViewer.refresh();
				tableShell.setVisible(true);
			}

		};

		autoComText.addListener(SWT.ARROW_DOWN, new Listener() {
			@Override
			public void handleEvent(Event arg0) {
				tableShell.setFocus();
			}
		});
		autoComText.addKeyListener(listener);
		autoComText.addFocusListener(focusListener);
	}

	/**
	 * Create populate right composite(InfoBox)
	 * 
	 * @param rightInfoBoxComposite
	 * @param label
	 */
	protected static void createRightComposite1(
			Composite rightInfoBoxComposite, String label) {
		rightInfoBoxComposite.setLayout(new GridLayout(1, true));
		Control childControl[] = rightInfoBoxComposite.getChildren();
		for (Control child : childControl) {
			child.dispose();
		}

		if (label.equals("Label 1")) {
			@SuppressWarnings("unused")
			Label1InfoBox firstComposite = new Label1InfoBox(
					rightInfoBoxComposite, SWT.NONE);
		} else {
			@SuppressWarnings("unused")
			OtherLabelInfoBox secondComposite = new OtherLabelInfoBox(
					rightInfoBoxComposite, SWT.NONE);
		}

		//
		rightInfoBoxComposite.layout();

	}

	/**
	 * Populate Right composite on basis of Label selected in left composite
	 * 
	 * @param rightInfoBoxComposite
	 * @param inputText
	 */
	static void createRightComposite(Composite rightInfoBoxComposite,
			String inputText) {

		Control childControl[] = rightInfoBoxComposite.getChildren();
		for (Control child : childControl) {
			child.dispose();
		}

		final Display display = rightInfoBoxComposite.getDisplay();
		rightInfoBoxComposite.setLayout(new FillLayout());

		ExpandBar expandBar = new ExpandBar(rightInfoBoxComposite, SWT.NONE);
		final ExpandItem expandItem1 = new ExpandItem(expandBar, SWT.NONE);
		expandItem1.setText("item 1");
		final StyledText text = new StyledText(expandBar, SWT.MULTI | SWT.WRAP);
		expandItem1.setControl(text);
		text.setText("initial text that will wrap if it's long enough");

		final ExpandItem expandItem2 = new ExpandItem(expandBar, SWT.NONE); /* expandItem2 */
		expandItem2.setText("item 2");
		expandItem2.setHeight(rightInfoBoxComposite.computeSize(SWT.DEFAULT,
				SWT.DEFAULT).y);

		if (inputText.equals("Label 1")) {
			expandItem2.setControl(new Label1InfoBox(expandBar, SWT.NONE));
		} else if (inputText.equals("Label 2")) {
			expandItem2.setControl(new OtherLabelInfoBox(expandBar, SWT.NONE));
		}
		expandItem2.setExpanded(true);

		/*
		 * update the item's height if needed in response to changes in the
		 * text's size
		 */
		final int TRIAL_WIDTH = 100;
		final int trimWidth = text.computeTrim(0, 0, TRIAL_WIDTH, 100).width
				- TRIAL_WIDTH;
		text.addListener(SWT.Modify, new Listener() {
			public void handleEvent(Event event) {
				Point size = text.computeSize(text.getSize().x - trimWidth,
						SWT.DEFAULT);
				if (expandItem1.getHeight() != size.y) {
					expandItem1.setHeight(size.y);
				}
			}
		});
		expandBar.addListener(SWT.Resize, new Listener() {
			public void handleEvent(Event event) {
				/*
				 * The following is done asynchronously to allow the Text's
				 * width to be changed before re-calculating its preferred
				 * height.
				 */
				display.asyncExec(new Runnable() {
					public void run() {
						if (text.isDisposed())
							return;
						Point size = text.computeSize(text.getSize().x
								- trimWidth, SWT.DEFAULT);
						if (expandItem1.getHeight() != size.y) {
							expandItem1.setHeight(size.y);
						}
					}
				});
			}
		});

		/* set the item's initial height */
		Point size = text.computeSize(expandBar.getClientArea().width,
				SWT.DEFAULT);
		expandItem1.setHeight(size.y);
		expandItem1.setExpanded(true);
		rightInfoBoxComposite.layout();
	}

}
